#!/bin/sh

TARGET=/tmp/Musik/flac

INPUT=$1
if [ ! -f "${INPUT}" ]; then
	echo Input file missing
	exit 1
fi

AUTHOR=$(metaflac --show-tag=ARTIST "${INPUT}"|cut -d= -f2|sed -e 's/^[\ ]*//g;s/\ /_/g;s/\///g')
echo $AUTHOR
ALBUM=$(metaflac --show-tag=ALBUM "${INPUT}"|cut -d= -f2|sed -e 's/^[\ ]*//g;s/\ /_/g;s/,//g;s/\///g')
echo $ALBUM
TITLE=$(metaflac --show-tag=TITLE "${INPUT}"|cut -d= -f2|cut -d\( -f1 |sed -e 's/^[\ ]*//g;s/\ /_/g;s/,//g;s/\///g;s/\&//g'|sed -e 's/__/_/g'|sed -e 's/_$//g')
echo $TITLE
TRACK=$(printf "%03d" $(metaflac --show-tag=TRACKNUMBER "${INPUT}"|cut -d= -f2|sed -e 's/^[\ ]*//g'))
echo $TRACK

if [ ! -d "${TARGET}"/"${AUTHOR}"/"${ALBUM}" ]; then
	mkdir -p "${TARGET}"/"${AUTHOR}"/"${ALBUM}"
fi

cp --reflink=always "${INPUT}" "${TARGET}"/"${AUTHOR}"/"${ALBUM}"/${TRACK}_"${TITLE}".flac
